Steps:
# import the necessary packages
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import Activation
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras import backend as K
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split
from imutils import paths
import matplotlib.pyplot as plt
import numpy as np
import argparse
import random
import cv2
import os
import warnings
from tqdm import tqdm_notebook as tqdm
import itertools
warnings.filterwarnings("ignore")
SEED = 42 # set random seed
# create CNN Model
class LeNet:
@staticmethod
def build(width, height, depth, classes):
# initialize the model
model = Sequential()
inputShape = (height, width, depth)
# if we are using "channels first", update the input shape
if K.image_data_format() == "channels_first":
inputShape = (depth, height, width)
# first set of CONV => RELU => POOL layers
model.add(Conv2D(20, (5, 5), padding="same",input_shape=inputShape))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
# second set of CONV => RELU => POOL layers
model.add(Conv2D(50, (5, 5), padding="same"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
# first (and only) set of FC => RELU layers
model.add(Flatten())
model.add(Dense(500))
model.add(Activation("relu"))
# softmax classifier
model.add(Dense(classes))
model.add(Activation("softmax"))
# return the constructed network architecture
return model
DATASET = "DATASET" # this folde must contain three subfolder with images
MODEL = "Scene.model" # name to store the model on disk
PLOT = "plot.png" # plot name
# initialize the number of epochs to train for, initia learning rate,
# and batch size
EPOCHS = 50
INIT_LR = 1e-3
BS = 32
# initialize the data and labels
print("[INFO] loading images...")
data = []
labels = []
# grab the image paths and randomly shuffle them
imagePaths = sorted(list(paths.list_images(DATASET)))
random.seed(42)
random.shuffle(imagePaths)
# progress bar
with tqdm(total=len(imagePaths)) as pbar:
# loop over the input images
for idx, imagePath in enumerate(imagePaths):
# load the image, pre-process it, and store it in the data list
image = cv2.imread(imagePath)
image = cv2.resize(image, (28, 28))
image = img_to_array(image)
data.append(image)
# extract the class label from the image path and update the
# labels list
label = imagePath.split(os.path.sep)[-2]
# label = 1 if label == "Good_pizza" else 0
# print("ex: ", label)
if label == "Buildings":
label = 0
elif label == "Forest":
label = 1
elif label == "Sea":
label = 2
# print("pr: ", label)
labels.append(label)
# update the progressbar
pbar.update(1)
[INFO] loading images...
# scale the raw pixel intensities to the range [0, 1]
data = np.array(data, dtype="float") / 255.0
labels = np.array(labels)
# partition the data into training and testing splits using 75% of
# the data for training and the remaining 25% for testing
(trainX, testX, trainY, testY) = train_test_split(data, labels, test_size=0.25, random_state=42)
# convert the labels from integers to vectors
trainY = to_categorical(trainY, num_classes=3)
testY = to_categorical(testY, num_classes=3)
trainY[0]
array([1., 0., 0.], dtype=float32)
# construct the image generator for data augmentation
aug = ImageDataGenerator(rotation_range=30,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode="nearest")
# initialize the model
print("[INFO] compiling model...")
model = LeNet.build(width=28, height=28, depth=3, classes=3)
opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
print("[INFO] model complied...")
[INFO] compiling model... [INFO] model complied...
print("[summary]")
print(model.summary())
[summary] Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d (Conv2D) (None, 28, 28, 20) 1520 _________________________________________________________________ activation (Activation) (None, 28, 28, 20) 0 _________________________________________________________________ max_pooling2d (MaxPooling2D) (None, 14, 14, 20) 0 _________________________________________________________________ conv2d_1 (Conv2D) (None, 14, 14, 50) 25050 _________________________________________________________________ activation_1 (Activation) (None, 14, 14, 50) 0 _________________________________________________________________ max_pooling2d_1 (MaxPooling2 (None, 7, 7, 50) 0 _________________________________________________________________ flatten (Flatten) (None, 2450) 0 _________________________________________________________________ dense (Dense) (None, 500) 1225500 _________________________________________________________________ activation_2 (Activation) (None, 500) 0 _________________________________________________________________ dense_1 (Dense) (None, 3) 1503 _________________________________________________________________ activation_3 (Activation) (None, 3) 0 ================================================================= Total params: 1,253,573 Trainable params: 1,253,573 Non-trainable params: 0 _________________________________________________________________ None
# train the network
print("[INFO] training network...")
H = model.fit(x=aug.flow(trainX, trainY, batch_size=BS),
validation_data=(testX, testY),
steps_per_epoch=len(trainX) // BS,
epochs=EPOCHS,
verbose=1)
[INFO] training network... Train for 31 steps, validate on 332 samples Epoch 1/50 31/31 [==============================] - 17s 562ms/step - loss: 0.8441 - accuracy: 0.6040 - val_loss: 0.6350 - val_accuracy: 0.7319 Epoch 2/50 31/31 [==============================] - 6s 196ms/step - loss: 0.7118 - accuracy: 0.6985 - val_loss: 0.7003 - val_accuracy: 0.6325 Epoch 3/50 31/31 [==============================] - 7s 222ms/step - loss: 0.6174 - accuracy: 0.7578 - val_loss: 0.4699 - val_accuracy: 0.7982 Epoch 4/50 31/31 [==============================] - 6s 210ms/step - loss: 0.5845 - accuracy: 0.7568 - val_loss: 0.5788 - val_accuracy: 0.7470 Epoch 5/50 31/31 [==============================] - 7s 212ms/step - loss: 0.5500 - accuracy: 0.7963 - val_loss: 0.4854 - val_accuracy: 0.7831 Epoch 6/50 31/31 [==============================] - 7s 217ms/step - loss: 0.4558 - accuracy: 0.8191 - val_loss: 0.3956 - val_accuracy: 0.8434 Epoch 7/50 31/31 [==============================] - 6s 208ms/step - loss: 0.4552 - accuracy: 0.8264 - val_loss: 0.5626 - val_accuracy: 0.7500 Epoch 8/50 31/31 [==============================] - 7s 219ms/step - loss: 0.4377 - accuracy: 0.8389 - val_loss: 0.4441 - val_accuracy: 0.8163 Epoch 9/50 31/31 [==============================] - 3s 100ms/step - loss: 0.3850 - accuracy: 0.8545 - val_loss: 0.3832 - val_accuracy: 0.8524 Epoch 10/50 31/31 [==============================] - 6s 201ms/step - loss: 0.4572 - accuracy: 0.8046 - val_loss: 0.5808 - val_accuracy: 0.7349 Epoch 11/50 31/31 [==============================] - 7s 224ms/step - loss: 0.4332 - accuracy: 0.8347 - val_loss: 0.5119 - val_accuracy: 0.7651 Epoch 12/50 31/31 [==============================] - 7s 216ms/step - loss: 0.3837 - accuracy: 0.8368 - val_loss: 0.4975 - val_accuracy: 0.7801 Epoch 13/50 31/31 [==============================] - 7s 220ms/step - loss: 0.3853 - accuracy: 0.8534 - val_loss: 0.5384 - val_accuracy: 0.7620 Epoch 14/50 31/31 [==============================] - 7s 223ms/step - loss: 0.3734 - accuracy: 0.8503 - val_loss: 0.4143 - val_accuracy: 0.8283 Epoch 15/50 31/31 [==============================] - 7s 217ms/step - loss: 0.3624 - accuracy: 0.8701 - val_loss: 0.4372 - val_accuracy: 0.8283 Epoch 16/50 31/31 [==============================] - 6s 209ms/step - loss: 0.3467 - accuracy: 0.8607 - val_loss: 0.5220 - val_accuracy: 0.7651 Epoch 17/50 31/31 [==============================] - 7s 233ms/step - loss: 0.3368 - accuracy: 0.8628 - val_loss: 0.4354 - val_accuracy: 0.8193 Epoch 18/50 31/31 [==============================] - 8s 250ms/step - loss: 0.3622 - accuracy: 0.8441 - val_loss: 0.3417 - val_accuracy: 0.8584 Epoch 19/50 31/31 [==============================] - 3s 88ms/step - loss: 0.2951 - accuracy: 0.8857 - val_loss: 0.3676 - val_accuracy: 0.8554 Epoch 20/50 31/31 [==============================] - 7s 213ms/step - loss: 0.3193 - accuracy: 0.8784 - val_loss: 0.3368 - val_accuracy: 0.8524 Epoch 21/50 31/31 [==============================] - 6s 207ms/step - loss: 0.2804 - accuracy: 0.8909 - val_loss: 0.3664 - val_accuracy: 0.8614 Epoch 22/50 31/31 [==============================] - 7s 220ms/step - loss: 0.3395 - accuracy: 0.8628 - val_loss: 0.5530 - val_accuracy: 0.8012 Epoch 23/50 31/31 [==============================] - 7s 223ms/step - loss: 0.3265 - accuracy: 0.8701 - val_loss: 0.5169 - val_accuracy: 0.7771 Epoch 24/50 31/31 [==============================] - 3s 106ms/step - loss: 0.2956 - accuracy: 0.8815 - val_loss: 0.5683 - val_accuracy: 0.8072 Epoch 25/50 31/31 [==============================] - 2s 68ms/step - loss: 0.2863 - accuracy: 0.8836 - val_loss: 0.3610 - val_accuracy: 0.8404 Epoch 26/50 31/31 [==============================] - 2s 69ms/step - loss: 0.2974 - accuracy: 0.8711 - val_loss: 0.5527 - val_accuracy: 0.7711 Epoch 27/50 31/31 [==============================] - 2s 71ms/step - loss: 0.2889 - accuracy: 0.8794 - val_loss: 0.4685 - val_accuracy: 0.8434 Epoch 28/50 31/31 [==============================] - 2s 75ms/step - loss: 0.2887 - accuracy: 0.8815 - val_loss: 0.4737 - val_accuracy: 0.7892 Epoch 29/50 31/31 [==============================] - 2s 78ms/step - loss: 0.2633 - accuracy: 0.8909 - val_loss: 0.4594 - val_accuracy: 0.8163 Epoch 30/50 31/31 [==============================] - 2s 77ms/step - loss: 0.2584 - accuracy: 0.8888 - val_loss: 0.4461 - val_accuracy: 0.8283 Epoch 31/50 31/31 [==============================] - 2s 72ms/step - loss: 0.2560 - accuracy: 0.8919 - val_loss: 0.5715 - val_accuracy: 0.7711 Epoch 32/50 31/31 [==============================] - 2s 68ms/step - loss: 0.2544 - accuracy: 0.9002 - val_loss: 0.4825 - val_accuracy: 0.8313 Epoch 33/50 31/31 [==============================] - 2s 70ms/step - loss: 0.2437 - accuracy: 0.8919 - val_loss: 0.5490 - val_accuracy: 0.8193 Epoch 34/50 31/31 [==============================] - 2s 73ms/step - loss: 0.2751 - accuracy: 0.8898 - val_loss: 0.3753 - val_accuracy: 0.8645 Epoch 35/50 31/31 [==============================] - 2s 72ms/step - loss: 0.2123 - accuracy: 0.9210 - val_loss: 0.5846 - val_accuracy: 0.7861 Epoch 36/50 31/31 [==============================] - 3s 84ms/step - loss: 0.1997 - accuracy: 0.9220 - val_loss: 0.5381 - val_accuracy: 0.8193 Epoch 37/50 31/31 [==============================] - 3s 84ms/step - loss: 0.1980 - accuracy: 0.9200 - val_loss: 0.3515 - val_accuracy: 0.8825 Epoch 38/50 31/31 [==============================] - 3s 83ms/step - loss: 0.2786 - accuracy: 0.8857 - val_loss: 0.4991 - val_accuracy: 0.8133 Epoch 39/50 31/31 [==============================] - 2s 77ms/step - loss: 0.2776 - accuracy: 0.8846 - val_loss: 0.6184 - val_accuracy: 0.7801 Epoch 40/50 31/31 [==============================] - 2s 79ms/step - loss: 0.2712 - accuracy: 0.8846 - val_loss: 0.6271 - val_accuracy: 0.7620 Epoch 41/50 31/31 [==============================] - 3s 83ms/step - loss: 0.2488 - accuracy: 0.8971 - val_loss: 0.5413 - val_accuracy: 0.7771 Epoch 42/50 31/31 [==============================] - 3s 83ms/step - loss: 0.2236 - accuracy: 0.9044 - val_loss: 0.3908 - val_accuracy: 0.8524 Epoch 43/50 31/31 [==============================] - 3s 83ms/step - loss: 0.2076 - accuracy: 0.9200 - val_loss: 0.4474 - val_accuracy: 0.8193 Epoch 44/50 31/31 [==============================] - 3s 82ms/step - loss: 0.2183 - accuracy: 0.9148 - val_loss: 0.4379 - val_accuracy: 0.8283 Epoch 45/50 31/31 [==============================] - 3s 82ms/step - loss: 0.2402 - accuracy: 0.8981 - val_loss: 0.3971 - val_accuracy: 0.8645 Epoch 46/50 31/31 [==============================] - 3s 81ms/step - loss: 0.2926 - accuracy: 0.8867 - val_loss: 0.3752 - val_accuracy: 0.8705 Epoch 47/50 31/31 [==============================] - 2s 78ms/step - loss: 0.1892 - accuracy: 0.9189 - val_loss: 0.4422 - val_accuracy: 0.8404 Epoch 48/50 31/31 [==============================] - 3s 84ms/step - loss: 0.1890 - accuracy: 0.9262 - val_loss: 0.3904 - val_accuracy: 0.8705 Epoch 49/50 31/31 [==============================] - 3s 84ms/step - loss: 0.1509 - accuracy: 0.9356 - val_loss: 0.4154 - val_accuracy: 0.8494 Epoch 50/50 31/31 [==============================] - 3s 82ms/step - loss: 0.2089 - accuracy: 0.9252 - val_loss: 0.4342 - val_accuracy: 0.8434
# save the model to disk
print("[INFO] serializing network...")
model.save(MODEL, save_format="h5")
[INFO] serializing network...
# plot the training and validation accuracy
N = np.arange(0, EPOCHS)
plt.style.use("ggplot")
plt.figure(figsize = [10,8])
plt.plot(N, H.history["accuracy"], label="train_acc")
plt.plot(N, H.history["val_accuracy"], label="val_acc")
plt.title("CNN: Training and Validation Accuracy")
plt.xlabel("Epoch #", weight="bold")
plt.ylabel("Accuracy", weight="bold")
plt.legend()
plt.show()
# plot the training and validation loss
N = np.arange(0, EPOCHS)
plt.style.use("ggplot")
plt.figure(figsize = [10,8])
plt.plot(N, H.history["loss"], label="train_loss")
plt.plot(N, H.history["val_loss"], label="val_loss")
plt.title("CNN: Training & Validation Loss")
plt.xlabel("Epoch #", weight="bold")
plt.ylabel("Loss", weight="bold")
plt.legend()
plt.show()
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.models import load_model
import numpy as np
import argparse
import imutils
import cv2
import matplotlib.pyplot as plt
%matplotlib inline
def display_img(img):
fig = plt.figure(figsize=(12,10))
plt.grid(b=None)
ax = fig.add_subplot(111)
ax.imshow(img)
# import the necessary packages
from tensorflow.keras.models import load_model
import pickle
import cv2
# # load the model
print("[INFO] loading network and...")
model = load_model(MODEL)
# grab the image paths and randomly shuffle themt
testImagePaths = sorted(list(paths.list_images('test_examples'))) # data folder with 2 categorical folders
all_class = ["Buildings", "Forest", "Sea"]
# progress bar
with tqdm(total=len(testImagePaths)) as pbar:
for imagePath in testImagePaths:
# load the image
image = cv2.imread(imagePath)
orig = image.copy()
# pre-process the image for classification
image = cv2.resize(image, (28, 28))
image = image.astype("float") / 255.0
image = img_to_array(image)
image = np.expand_dims(image, axis=0)
# classify the input image
prd_conf = model.predict(image)[0]
# build the label
label = all_class[np.argmax(prd_conf)]
proba = prd_conf[np.argmax(prd_conf)]
label = "{}: {:.2f}%".format(label, proba * 100)
# draw the label on the image
output = imutils.resize(orig, width=400)
cv2.putText(output, label, (10, 25), cv2.FONT_HERSHEY_SIMPLEX,
0.7, (0, 255, 0), 2)
# convert img to rgb format and display in notebook
img = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
display_img(img)
pbar.update(1)
[INFO] loading network and...
import gradio as gr
def predict_image(image):
# load the image
# pre-process the image for classification
image = cv2.resize(image, (28, 28))
image = image.astype("float") / 255.0
image = img_to_array(image)
image = np.expand_dims(image, axis=0)
preds = model.predict(image)[0]
result = dict()
result["Buildings"] = round(float(list(preds)[0]), 3)
result["Forest"] = round(float(list(preds)[1]), 3)
result["Sea"] = round(float(list(preds)[2]), 3)
print(result)
return result
im = gr.inputs.Image(shape=(32,32))
label = gr.outputs.Label(num_top_classes=3)
gr.Interface(fn=predict_image, inputs=im, outputs=label, capture_session=True, title="CNN Demo").launch(share=True)
Running locally at: http://127.0.0.1:7869/ This share link will expire in 24 hours. If you need a permanent link, visit: https://gradio.app/introducing-hosted (NEW!) Running on External URL: https://24201.gradio.app Interface loading below...
(<Flask 'gradio.networking'>, 'http://127.0.0.1:7869/', 'https://24201.gradio.app')
{'Buildings': 0.039, 'Forest': 0.961, 'Sea': 0.0}